package com.ruoyi.rsikManage.service.Impl;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.rsikManage.domain.AlarmEvent;
import com.ruoyi.rsikManage.service.JyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.ruoyi.rsikManage.mapper.TVideoMessageQueueMapper;
import com.ruoyi.rsikManage.domain.TVideoMessageQueue;
import com.ruoyi.rsikManage.service.ITVideoMessageQueueService;

/**
 * 视频转换消息队列Service业务层处理
 *
 * @author sxygsj
 * @date 2024-09-10
 */
@Service
public class TVideoMessageQueueServiceImpl implements ITVideoMessageQueueService
{
    @Autowired
    private TVideoMessageQueueMapper tVideoMessageQueueMapper;
    @Autowired
    private JyService jyService;

    @Value("${system.videoFilePath}")
    String videoFilePath;

    @Value("${system.videoProxyFilePath}")
    String videoProxyFilePath;
    /**
     * 查询视频转换消息队列
     *
     * @param id 视频转换消息队列主键
     * @return 视频转换消息队列
     */
    @Override
    public TVideoMessageQueue selectTVideoMessageQueueById(Long id)
    {
        return tVideoMessageQueueMapper.selectTVideoMessageQueueById(id);
    }

    /**
     * 查询视频转换消息队列列表
     *
     * @param tVideoMessageQueue 视频转换消息队列
     * @return 视频转换消息队列
     */
    @Override
    public List<TVideoMessageQueue> selectTVideoMessageQueueList(TVideoMessageQueue tVideoMessageQueue)
    {
        return tVideoMessageQueueMapper.selectTVideoMessageQueueList(tVideoMessageQueue);
    }

    /**
     * 新增视频转换消息队列
     *
     * @param tVideoMessageQueue 视频转换消息队列
     * @return 结果
     */
    @Override
    public int insertTVideoMessageQueue(TVideoMessageQueue tVideoMessageQueue)
    {
        return tVideoMessageQueueMapper.insertTVideoMessageQueue(tVideoMessageQueue);
    }

    /**
     * 修改视频转换消息队列
     *
     * @param tVideoMessageQueue 视频转换消息队列
     * @return 结果
     */
    @Override
    public int updateTVideoMessageQueue(TVideoMessageQueue tVideoMessageQueue)
    {
        tVideoMessageQueue.setUpdateTime(LocalDateTime.now());
        return tVideoMessageQueueMapper.updateTVideoMessageQueue(tVideoMessageQueue);
    }

    /**
     * 批量删除视频转换消息队列
     *
     * @param ids 需要删除的视频转换消息队列主键
     * @return 结果
     */
    @Override
    public int deleteTVideoMessageQueueByIds(Long[] ids)
    {
        return tVideoMessageQueueMapper.deleteTVideoMessageQueueByIds(ids);
    }

    /**
     * 删除视频转换消息队列信息
     *
     * @param id 视频转换消息队列主键
     * @return 结果
     */
    @Override
    public int deleteTVideoMessageQueueById(Long id)
    {
        return tVideoMessageQueueMapper.deleteTVideoMessageQueueById(id);
    }

    private ExecutorService executorService; // 创建一个线程池
    public TVideoMessageQueueServiceImpl() {
        // 初始化线程池
        this.executorService = Executors.newSingleThreadExecutor();
    }
    @Override
    public AjaxResult videoConversion(AlarmEvent alarmEvent) {

        String deviceId = alarmEvent.getDeviceId();
        Date alarmTime = alarmEvent.getAlarmTime();

        // 创建一个Calendar实例，并设置为给定的时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(alarmTime);

        // 减少五秒
        calendar.add(Calendar.SECOND, -10);
        String alarmTimeMinusFiveSeconds = format(calendar.getTime());

        // 增加五秒
        calendar.add(Calendar.SECOND, 20); // 先加上10秒，因为前面减少了5秒
        String alarmTimePlusFiveSeconds = format(calendar.getTime());

        System.out.println("原始时间: " + alarmTime);
        System.out.println("减去五秒后的时间: " + alarmTimeMinusFiveSeconds);
        System.out.println("增加五秒后的时间: " + alarmTimePlusFiveSeconds);
        UUID uuid = UUID.randomUUID();

        String  outputFile=videoFilePath+"\\old\\"+uuid+".mp4";
        String  outputProxyFile=videoProxyFilePath+"\\old\\"+uuid+".mp4";

        String  newFile= videoFilePath+"\\new\\"+uuid+".mp4";
        String  newProxyFile= videoProxyFilePath+"\\new\\"+uuid+".mp4";

        // 异步执行getRecordStreamDownload方法
        executorService.submit(() -> {
            jyService.getRecordStreamDownload(deviceId, alarmTimeMinusFiveSeconds, alarmTimePlusFiveSeconds, outputFile);
        });
        TVideoMessageQueue tVideoMessageQueue = new TVideoMessageQueue();
        tVideoMessageQueue.setVideoOldPath(outputFile);
        tVideoMessageQueue.setVideoNewPath(newFile);
        tVideoMessageQueue.setVideoOldProxyPath(outputProxyFile);
        tVideoMessageQueue.setVideoNewProxyPath(newProxyFile);
        tVideoMessageQueue.setAlarmTypeName(alarmEvent.getAlarmTypeName());
        tVideoMessageQueue.setAlarmId(alarmEvent.getIdId());

        tVideoMessageQueueMapper.insertTVideoMessageQueue(tVideoMessageQueue);


        System.out.println("返回空列表，或者根据业务需求返回其他结果:" + LocalDateTime.now());

        // 返回空列表，或者根据业务需求返回其他结果
        return new AjaxResult(200,"开始转换"); // 或者返回null，取决于您的业务逻辑
    }


    /**
     * 将Date对象格式化为字符串
     * @param date 需要格式化的日期
     * @return 格式化后的字符串
     */
    private static String format(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 指定格式
        return sdf.format(date);
    }
}
